/************************************
 * created by Tonk (tonk@ctonk.com) *
 ************************************/

// note.cpp
#include "note.h"
#include <fstream>
#include <string>
#include "stringutil.h"

int Note::noteIndex = 0;
int Note::numNotes = 0;

Note::Note() {
	setId( ++noteIndex );
	setPid( 0 );
	numNotes++;
}

Note::Note( Note& it ) {
	setId( it.getId() );
	setPid( it.getPid() );
	setBoard( it.getBoard() );
	setAuthor( it.getAuthor() );
	setRecipient( it.getRecipient() );
	setSubject( it.getSubject() );
	setBody( it.getBody() );
	setSent( it.getSent() );
}

Note::~Note() {
	numNotes--;
}

Note& Note::operator = ( Note& it ) {
	setId( it.getId() );
	setPid( it.getPid() );
	setBoard( it.getBoard() );
	setAuthor( it.getAuthor() );
	setRecipient( it.getRecipient() );
	setSubject( it.getSubject() );
	setBody( it.getBody() );
	setSent( it.getSent() );
	return *this;
}

void Note::setId( int id ) {
	_id = ( id > 0 ) ? id : 0;
	return;
}

int Note::getId() {
	return _id;
}

void Note::setPid( int pid ) {
	_pid = ( pid > 0 ) ? pid : 0;
	return;
}

int Note::getPid() {
	return _pid;
}

void Note::setBoard( std::string board ) {
	_board = board;
	return;
}

std::string Note::getBoard() {
	return _board;
}

void Note::setAuthor( std::string author ) {
	_author = author;
	return;
}

std::string Note::getAuthor() {
	return _author;
}

void Note::setRecipient( std::string recipient ) {
	_recipient = recipient;
	return;
}

std::string Note::getRecipient() {
	return _recipient;
}

void Note::setSubject( std::string subject ) {
	_subject = subject;
}

std::string Note::getSubject() {
	return _subject;
}

void Note::setBody( std::string body ) {
	_body = body;
}

std::string& Note::getBody() {
	return _body;
}

void Note::setSent( std::string sent ) {
	_sent = sent;
}

std::string Note::getSent() {
	return _sent;
}

void Note::save( std::ofstream& save ) {
	save << "\n**NOTE\n";
	save << getId() << ' ' << getPid() << '\n';
	save << getBoard() << '\n';
	save << getAuthor() << '\n';
	save << getRecipient() << '\n';
	save << getSubject() << '\n';
	save << getSent() << '\n';
	save << getBody() << "$\n";
	return;
}

void Note::load( std::ifstream& load ) {
	int itmp = 0;
	char ctmp[100000];
	load >> itmp;
	setId( itmp );
	load >> itmp;
	setPid( itmp );
	load.get();
	load.getline( ctmp, 100000 );
	setBoard( to_str(ctmp) );
	load.getline( ctmp, 100000 );
	setAuthor( to_str(ctmp) );
	load.getline( ctmp, 100000 );
	setRecipient( to_str(ctmp) );
	load.getline( ctmp, 100000 );
	setSubject( to_str(ctmp) );
	load.getline( ctmp, 100000 );
	setSent( to_str(ctmp) );
	load.getline( ctmp, 100000, '$' );
	setBody( to_str(ctmp) );
	return;
}
